<?php

namespace App\Admin\Controllers;
use App\Http\Controllers\Controller;
use App\Models\Student;
use App\Models\InviteLog;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;

class InviteController extends Controller
{
	public function regist(Request $request)
	{
		$sales_id = $request->sales_id ?? 0;
		$phone = $request->phone ?? '';
		$age = $request->age ?? 0;
		$begin_control = $request->degree ?? 0;
		if (!preg_match('/^1[3-9]\d{9}$/', $phone)) {
			return $this->error('亲，请填写正确手机号');
		}
		if ($phone == '') {
			return $this->error('亲，请填写正确手机号');
		}
		// if ($age <= 0 || $age >= 15) {
		// 	return $this->error('抱歉，孩子年龄不符合活动要求【入组标准：年龄：6-14岁】');
		// }
		// if (!is_numeric($begin_control)) {
		// 	return $this->error('亲，请填写正确的度数，如：250');
		// }
		// if ($begin_control < 50 || $begin_control > 600) {
		// 	return $this->error('抱歉，孩子目前度数不符合入组标准【入组标准：度数：50-600度，散光小于200度，近视度数差值不能高于200度】');
		// }
		$s = Student::where('phone', $phone)->exists();
		if ($s) {
			return $this->success('亲，您已报名成功~');
		}
		try {
			DB::beginTransaction();
			$sid = Student::addInvite($phone, $age, $begin_control);
			InviteLog::addLog($sales_id, $sid, InviteLog::REGIST);
			DB::commit();
			return $this->success('报名成功');
		} catch (\Exception $e) {
			return $this->error($e->getMessage());
		}
	}

	public function scan(Request $request)
	{
		$sales_id = $request->sales_id ?? 0;
		InviteLog::addLog($sales_id, 0, InviteLog::SCAN);
		return $this->success();
	}
}